﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace XrmLibrary.ConfigFile
{

    [ConfigurationCollection(typeof(AdapterCollection), AddItemName=AdapterConfigurationsCollection.ItemName, CollectionType=ConfigurationElementCollectionType.BasicMap)]
    public class AdapterConfigurationsCollection : ConfigurationElementCollection
    {
        private const String ItemName = "adapter";

        private static ConfigurationPropertyCollection _properties;

        #region Constructors
        static AdapterConfigurationsCollection()
        {
            _properties = new ConfigurationPropertyCollection();
        }

        public AdapterConfigurationsCollection()
        {}
        #endregion

        #region Properties
        protected override ConfigurationPropertyCollection Properties
        {
            get { return _properties; }
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }

        protected override string ElementName
        {
            get { return ItemName; }
        }
        #endregion


        #region Indexers
        public AdapterCollection this[Int32 index]
        {
            get { return (AdapterCollection)base.BaseGet(index); }
            set
            {
                if(base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                base.BaseAdd(index, value);
            }
        }


        public AdapterCollection this[String name]
        {
            get { return (AdapterCollection) base.BaseGet(name); }
        }
        #endregion


        protected override ConfigurationElement CreateNewElement()
        {
            return new AdapterCollection();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as AdapterCollection).AssemblyName;
        }

    }
}
